﻿using System;
using UnityEngine;

// Token: 0x0200020F RID: 527
public class WorldNodes
{
	// Token: 0x06000B14 RID: 2836 RVA: 0x0005DC08 File Offset: 0x0005BE08
	public WorldNodes(Vector3 virtualStart, float height, float tilewidth, float step, float angle)
	{
		this.angleMutiplier = angle;
		this.tileWidth = tilewidth;
		this.height = height;
		this.step = step;
		PathNode pathNode = this.NodeFromPoint(virtualStart);
		pathNode.walkable = true;
		this.checkCount = 0;
		this.down = Vector3.down;
		this.up = Vector3.up;
		this.pathfindMask = Layers.pathfindingMask;
		WorldNodes.instanceID++;
		int num = this.maxChecks + 1;
		this.arrayLength = num * 2;
		if (WorldNodes.nodeArray == null)
		{
			WorldNodes.nodeArray = new PathNode[this.arrayLength, this.arrayLength];
		}
		pathNode.instanceID = WorldNodes.instanceID;
		WorldNodes.nodeArray[num, num] = pathNode;
		this.vx = num - pathNode.x;
		this.vz = num - pathNode.z;
		Debug.DrawRay(pathNode.realPosition, this.up * height * 2f, Color.red, 10f);
	}

	// Token: 0x06000B15 RID: 2837 RVA: 0x00002973 File Offset: 0x00000B73
	public void UpdateNodes(Vector3 start, int radius)
	{
	}

	// Token: 0x06000B16 RID: 2838 RVA: 0x0005DD38 File Offset: 0x0005BF38
	public PathNode[] GetConnections(PathNode node)
	{
		if (this.checkCount < this.maxChecks && !node.visited)
		{
			this.FindNeighboors(node, 2);
			this.UpdateWalkability(node);
			node.visited = true;
			this.checkCount++;
		}
		return node.GetConnections();
	}

	// Token: 0x06000B17 RID: 2839 RVA: 0x0005DD8C File Offset: 0x0005BF8C
	private void FindNeighboors(PathNode node, int levels = 0)
	{
		for (int i = node.x - 1; i <= node.x + 1; i++)
		{
			for (int j = node.z - 1; j <= node.z + 1; j++)
			{
				if ((i != node.x || j != node.z) && (i == node.x || j == node.z))
				{
					PathNode pathNode = this.GetNodeIfExists(i, j);
					bool flag = false;
					if (pathNode == null)
					{
						pathNode = this.SampleNode(i, node.y, j);
					}
					else
					{
						if (pathNode.visited || pathNode.walkable)
						{
							goto IL_151;
						}
						if (this.NodesAreConnected(node, pathNode))
						{
							flag = true;
						}
					}
					if (pathNode != null)
					{
						if (!flag && this.CheckWalkability(node, pathNode))
						{
							flag = true;
							if (node.x != pathNode.x)
							{
								if (node.x > pathNode.x)
								{
									node.est = pathNode;
									pathNode.west = node;
								}
								else
								{
									node.west = pathNode;
									pathNode.est = node;
								}
							}
							else if (node.z > pathNode.z)
							{
								node.north = pathNode;
								pathNode.south = node;
							}
							else
							{
								node.south = pathNode;
								pathNode.north = node;
							}
						}
						if (flag && levels > 1)
						{
							this.FindNeighboors(pathNode, levels - 1);
						}
					}
				}
				IL_151:;
			}
		}
	}

	// Token: 0x06000B18 RID: 2840 RVA: 0x0005DF10 File Offset: 0x0005C110
	private void UpdateWalkability(PathNode node)
	{
		for (int i = node.x - 1; i <= node.x + 1; i++)
		{
			for (int j = node.z - 1; j <= node.z + 1; j++)
			{
				if (i != node.x || j != node.z)
				{
					PathNode nodeIfExists = this.GetNodeIfExists(i, j);
					if (nodeIfExists != null)
					{
						if (!nodeIfExists.walkable && i != node.x && j != node.z)
						{
							this.FindNeighboors(nodeIfExists, 0);
						}
						if (nodeIfExists.CanWalkAllDirections())
						{
							nodeIfExists.walkable = true;
						}
					}
				}
			}
		}
	}

	// Token: 0x06000B19 RID: 2841 RVA: 0x0005DFCB File Offset: 0x0005C1CB
	private bool NodesAreConnected(PathNode a, PathNode b)
	{
		return a.north == b || a.south == b || a.est == b || a.west == b;
	}

	// Token: 0x06000B1A RID: 2842 RVA: 0x00002973 File Offset: 0x00000B73
	public void DrawLineBetweenNodes(PathNode a, PathNode b, Color color, float time = 0f)
	{
	}

	// Token: 0x06000B1B RID: 2843 RVA: 0x0005E000 File Offset: 0x0005C200
	private PathNode GetNode(int x, int z)
	{
		int num = this.vx + x;
		int num2 = this.vz + z;
		if (num < 0 || num >= this.arrayLength || num2 < 0 || num2 >= this.arrayLength)
		{
			return new PathNode(x, z);
		}
		PathNode pathNode = WorldNodes.nodeArray[num, num2];
		if (pathNode == null)
		{
			pathNode = new PathNode(x, z);
			WorldNodes.nodeArray[num, num2] = pathNode;
			pathNode.instanceID = WorldNodes.instanceID;
		}
		else if (pathNode.instanceID != WorldNodes.instanceID)
		{
			pathNode.ResetValues(x, z);
			pathNode.instanceID = WorldNodes.instanceID;
			WorldNodes.nodeArray[num, num2] = pathNode;
		}
		return pathNode;
	}

	// Token: 0x06000B1C RID: 2844 RVA: 0x0005E0B8 File Offset: 0x0005C2B8
	public PathNode GetNodeIfExists(int x, int z)
	{
		int num = this.vx + x;
		int num2 = this.vz + z;
		if (num < 0 || num >= this.arrayLength || num2 < 0 || num2 >= this.arrayLength)
		{
			return null;
		}
		PathNode pathNode = WorldNodes.nodeArray[num, num2];
		if (pathNode != null && pathNode.instanceID == WorldNodes.instanceID)
		{
			return pathNode;
		}
		return null;
	}

	// Token: 0x06000B1D RID: 2845 RVA: 0x0005E128 File Offset: 0x0005C328
	private bool CheckWalkability(PathNode currentNode, PathNode previousNode)
	{
		if (previousNode == null)
		{
			return false;
		}
		float num = currentNode.y - previousNode.y;
		if (num < 0f)
		{
			num = -num;
		}
		if (num > this.tileWidth * this.angleMutiplier)
		{
			return false;
		}
		Vector3 realPosition = currentNode.realPosition;
		Vector3 realPosition2 = previousNode.realPosition;
		realPosition.y += this.step;
		realPosition2.y += this.step;
		Vector3 direction = this.Substract(realPosition2, realPosition);
		float magnitude = direction.magnitude;
		return !Physics.Raycast(realPosition, direction, magnitude, this.pathfindMask) && !Physics.Raycast(realPosition2, this.Substract(realPosition, realPosition2), magnitude, this.pathfindMask);
	}

	// Token: 0x06000B1E RID: 2846 RVA: 0x0005E1F4 File Offset: 0x0005C3F4
	private PathNode SampleNode(int x, float y, int z)
	{
		Vector3 origin = CenterOrigin.VirtualToWorld((float)x * this.tileWidth, y, (float)z * this.tileWidth);
		float num = (this.height + this.tileWidth) * this.angleMutiplier;
		origin.y += num;
		RaycastHit raycastHit;
		if (!Physics.Raycast(origin, this.down, out raycastHit, num * 2f, this.pathfindMask))
		{
			return null;
		}
		Vector3 point = raycastHit.point;
		float y2 = point.y;
		point.y += this.step;
		if (Physics.Raycast(point, this.up, this.height, this.pathfindMask))
		{
			return null;
		}
		PathNode node = this.GetNode(x, z);
		node.y = CenterOrigin.WorldToVirtual(0f, y2, 0f).y;
		node.realPosition.x = point.x;
		node.realPosition.y = y2;
		node.realPosition.z = point.z;
		return node;
	}

	// Token: 0x06000B1F RID: 2847 RVA: 0x0005E310 File Offset: 0x0005C510
	private Vector3 Substract(Vector3 a, Vector3 b)
	{
		a.x -= b.x;
		a.y -= b.y;
		a.z -= b.z;
		return a;
	}

	// Token: 0x06000B20 RID: 2848 RVA: 0x0005E360 File Offset: 0x0005C560
	private Vector3 Sum(Vector3 a, Vector3 b)
	{
		a.x += b.x;
		a.y += b.y;
		a.z += b.z;
		return a;
	}

	// Token: 0x06000B21 RID: 2849 RVA: 0x0005E3AD File Offset: 0x0005C5AD
	public Vector3 LocalizeCoordinates(PathNode node)
	{
		return CenterOrigin.VirtualToWorld((float)node.x * this.tileWidth, node.y, (float)node.z * this.tileWidth);
	}

	// Token: 0x06000B22 RID: 2850 RVA: 0x0005E3D8 File Offset: 0x0005C5D8
	public PathNode PointToNode(Vector3 point)
	{
		int x = this.QuantizeFloat(point.x);
		int z = this.QuantizeFloat(point.z);
		PathNode pathNode = this.SampleNode(x, point.y, z);
		if (pathNode != null)
		{
			return pathNode;
		}
		pathNode = this.GetNode(x, z);
		pathNode.y = point.y;
		pathNode.walkable = true;
		pathNode.realPosition = this.LocalizeCoordinates(pathNode);
		return pathNode;
	}

	// Token: 0x06000B23 RID: 2851 RVA: 0x0005E444 File Offset: 0x0005C644
	private PathNode NodeFromPoint(Vector3 point)
	{
		int x = this.QuantizeFloat(point.x);
		int z = this.QuantizeFloat(point.z);
		PathNode pathNode = new PathNode(x, z);
		pathNode.y = point.y;
		pathNode.walkable = true;
		pathNode.realPosition = this.LocalizeCoordinates(pathNode);
		return pathNode;
	}

	// Token: 0x06000B24 RID: 2852 RVA: 0x0005E498 File Offset: 0x0005C698
	private int QuantizeFloat(float f)
	{
		float num = f / this.tileWidth;
		if (num > 0f)
		{
			return (int)(num + 0.5f);
		}
		return (int)(num - 0.5f);
	}

	// Token: 0x04001035 RID: 4149
	private static PathNode[,] nodeArray;

	// Token: 0x04001036 RID: 4150
	private static int instanceID;

	// Token: 0x04001037 RID: 4151
	private int arrayLength;

	// Token: 0x04001038 RID: 4152
	private int vx;

	// Token: 0x04001039 RID: 4153
	private int vz;

	// Token: 0x0400103A RID: 4154
	private float tileWidth;

	// Token: 0x0400103B RID: 4155
	private float height;

	// Token: 0x0400103C RID: 4156
	private float step;

	// Token: 0x0400103D RID: 4157
	private float angleMutiplier = 0.5f;

	// Token: 0x0400103E RID: 4158
	private float rayDuration = 30f;

	// Token: 0x0400103F RID: 4159
	public int checkCount;

	// Token: 0x04001040 RID: 4160
	public int maxChecks = 1000;

	// Token: 0x04001041 RID: 4161
	private bool debugNodes = true;

	// Token: 0x04001042 RID: 4162
	private Vector3 down;

	// Token: 0x04001043 RID: 4163
	private Vector3 up;

	// Token: 0x04001044 RID: 4164
	private LayerMask pathfindMask;
}
